package com.fireline.beauty.member;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.wb.swt.SWTResourceManager;

import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.zgw.fireline.base.controls.DataTable;
import com.zgw.fireline.base.controls.DataTableColumn;
import com.zgw.fireline.base.dataset.Dataset;
import com.zgw.fireline.base.frame.FunctionDialog;
import com.zgw.fireline.base.frame.IFunction;
import com.zgw.fireline.base.widgets.Batch;
import com.zgw.fireline.base.widgets.BatchItem;

public class HyyuView implements IFunction {

	private Shell shell;
	private Text text;
	/**
	 * @wbp.nonvisual location=27,562
	 */
	private final Dataset yuxxSet = new Dataset(
			"select strftime('%m月%d日 %H:%M',a.kssj)||'至 '|| strftime('%H:%M',a.jssj) yusj, \r\na.*,\r\ncase \r\nwhen a.zt=0 then\r\n'已取消'\r\nwhen datetime(a.kssj)<datetime('now','localtime') then\r\n'已过期'\r\nwhen a.zt=1 then\r\n'正常'\r\nend ztsm\r\n,b.hyxm,c.ygmc from hyyu a \r\nleft join hyxx b on a.hybh=b.hybh\r\nleft join ygxx c on a.ygbh=c.ygbh\r\nwhere (?=1 or (a.zt=1 and datetime(a.kssj)>datetime('now','localtime')))  -- 有效预约必须当前时间之后\r\nand (? is null or b.hybh like '%'||?||'%' or b.hyxm like '%'||?||'%')",
			SqliteJdbcImpl.INSTANCE);
	private Button button;
	/**
	 * @wbp.nonvisual location=93,562
	 */
	private final Batch delBatch = new Batch(SqliteJdbcImpl.INSTANCE);
	/**
	 * @wbp.nonvisual location=163,562
	 */
	private final Batch qxBatch = new Batch(SqliteJdbcImpl.INSTANCE);

	/**
	 * @wbp.parser.entryPoint
	 */
	@Override
	public Control createControl(Composite parent) {
		this.shell = parent.getShell();
		yuxxSet.setKeyColumns(new String[] {});

		BatchItem batchItem = new BatchItem(delBatch);
		batchItem.setCommand("delete from hyyu where id=?");
		batchItem.setName("删除预约记录");

		BatchItem batchItem_1 = new BatchItem(qxBatch);
		batchItem_1
				.setCommand(" update hyyu set zt=0 ,bz=bz||? where id=?  ");
		batchItem_1.setName("取消预约");
		Composite content = new Composite(parent, SWT.NONE);
		content.setLayout(new GridLayout(1, false));

		ToolBar toolBar = new ToolBar(content, SWT.FLAT | SWT.RIGHT);
		toolBar.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false,
				1, 1));
		ToolItem tltmNewItem = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doAddYu();
			}
		});
		tltmNewItem.setText("预约");

		ToolItem tltmNewItem_1 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doQxyu();
			}
		});
		tltmNewItem_1.setText("取消预约");

		ToolItem tltmNewItem_2 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doDelete();
			}
		});
		tltmNewItem_2.setText("删除");

		ToolItem toolItem = new ToolItem(toolBar, SWT.NONE);
		toolItem.setText("退出");

		Composite composite = new Composite(content, SWT.NONE);
		composite.setLayout(new GridLayout(5, false));
		composite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		new Label(composite, SWT.NONE);

		Label label = new Label(composite, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label.setText("会员编号姓名：");

		text = new Text(composite, SWT.BORDER);
		GridData gd_text = new GridData(SWT.FILL, SWT.CENTER, false, false, 1,
				1);
		gd_text.widthHint = 100;
		text.setLayoutData(gd_text);

		ToolBar toolBar_1 = new ToolBar(composite, SWT.FLAT | SWT.RIGHT);

		ToolItem toolItem_1 = new ToolItem(toolBar_1, SWT.NONE);
		toolItem_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSearch();
			}
		});
		toolItem_1.setImage(SWTResourceManager.getImage(HyyuView.class,
				"/com/fireline/beauty/image/query.png"));
		toolItem_1.setText("查找");

		button = new Button(composite, SWT.CHECK);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSearch();
			}
		});
		button.setText("显示过期预约");

		TabFolder tabFolder = new TabFolder(content, SWT.NONE);
		tabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1,
				1));

		TabItem tbtmNewItem = new TabItem(tabFolder, SWT.NONE);
		tbtmNewItem.setText("预约列表");

		Composite composite_1 = new Composite(tabFolder, SWT.NONE);
		tbtmNewItem.setControl(composite_1);
		composite_1.setLayout(new GridLayout(1, false));

		DataTable dataTable = new DataTable(composite_1, SWT.BORDER
				| SWT.FULL_SELECTION, yuxxSet);
		dataTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1,
				1));
		dataTable.setHeaderVisible(true);
		dataTable.setLinesVisible(true);

		DataTableColumn dataTableColumn = new DataTableColumn(dataTable,
				SWT.NONE, "hyxm");
		dataTableColumn.setText("会员名称");
		dataTableColumn.setWidth(80);

		DataTableColumn dataTableColumn_1 = new DataTableColumn(dataTable,
				SWT.NONE, "yusj");
		dataTableColumn_1.setText("开始时间");
		dataTableColumn_1.setWidth(150);

		DataTableColumn dataTableColumn_3 = new DataTableColumn(dataTable,
				SWT.NONE, "xmmc");
		dataTableColumn_3.setText("项目");
		dataTableColumn_3.setWidth(80);

		DataTableColumn dataTableColumn_4 = new DataTableColumn(dataTable,
				SWT.NONE, "ygmc");
		dataTableColumn_4.setText("技师");
		dataTableColumn_4.setWidth(80);

		DataTableColumn dataTableColumn_5 = new DataTableColumn(dataTable,
				SWT.NONE, "ch");
		dataTableColumn_5.setText("床号");
		dataTableColumn_5.setWidth(80);

		DataTableColumn dataTableColumn_2 = new DataTableColumn(dataTable,
				SWT.NONE, "ztsm");
		dataTableColumn_2.setText("状态");
		dataTableColumn_2.setWidth(80);

		DataTableColumn dataTableColumn_6 = new DataTableColumn(dataTable,
				SWT.RIGHT, "fwsj");
		dataTableColumn_6.setText("服务时间（分钟）");
		dataTableColumn_6.setWidth(120);

		DataTableColumn dataTableColumn_7 = new DataTableColumn(dataTable,
				SWT.NONE, "bz");
		dataTableColumn_7.setText("备注");
		dataTableColumn_7.setWidth(80);

		TabItem tbtmNewItem_1 = new TabItem(tabFolder, SWT.NONE);
		tbtmNewItem_1.setText("预约状态");
		setParams();
		createGUIAfter();
		return content;
	}

	/*
	 * 取消预约
	 */
	protected void doQxyu() {
		setParams();
		if (yuxxSet.getSelectedIndex() == -1) {
			MessageDialog.openInformation(shell, "提示", "请选择一条记录");
			return;
		} else if (!"正常".equals(yuxxSet.getString("ztsm"))) {
			MessageDialog.openInformation(shell, "提示", "取消失败，该预约记录已过期或已经失效");
			return;
		} else if (MessageDialog.openQuestion(shell, "删除确定", "确认取消该预约?")) {
			try {
				qxBatch.execute();
				MessageDialog.openInformation(shell, "提示", "预约已取消");
				doSearch();
			} catch (Exception e) {
				e.printStackTrace();
				MessageDialog
						.openError(shell, "异常", "执行取消失败:" + e.getMessage());
			}
		}
	}

	protected void doDelete() {
		if (yuxxSet.getSelectedIndex() == -1) {
			MessageDialog.openInformation(shell, "提示", "请选择一条记录");
			return;
		} else if (MessageDialog.openQuestion(shell, "删除确定", "确认删除记录?")) {
			setParams();
			try {
				delBatch.execute();
			} catch (Exception e) {
				MessageDialog.openError(shell, "异常", "删除失败:" + e.getMessage());
			}
		}
	}

	protected void doAddYu() {
		HyyuEdit edit = new HyyuEdit();
		FunctionDialog dlg = new FunctionDialog(shell, edit);
		dlg.open();
		if (edit.result == SWT.OK) {
			doSearch();
		}
	}

	protected void doSearch() {
		setParams();
		yuxxSet.update();
	}

	protected void setParams() {
		delBatch.setParam(0, 0, yuxxSet.getValue("id"));
		yuxxSet.setParam(0, button.getSelection());
		yuxxSet.setParam(1, text.getText());
		yuxxSet.setParam(2, text.getText());
		yuxxSet.setParam(3, text.getText());
		qxBatch.setParam(0,0," 预约已取消");
		qxBatch.setParam(0,1,yuxxSet.getValue("id"));
	}

	protected void createGUIAfter() {
		yuxxSet.update();
	}

	public static void main(String[] args) {
		// System.out.println(SimpleDateFormat.getInstance().format(new
		// Date()));
		IFunction fun = new HyyuView();
		FunctionDialog dlg = new FunctionDialog(new Shell(), fun);
		dlg.open();
	}

}
